//
//  HomeSerachController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/16.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeSerachController.h"

@interface HomeSerachController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

/**<#属性#>*/
@property (nonatomic,strong)UIButton * rightBUtton;


@end

@implementation HomeSerachController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self setSerachView];
    [self.view addSubview:self.listView];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setIsHidenNaviBar:YES];
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray * aray = self.dataArray[section];
    return aray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeSerachOneItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(HomeSerachOneItem.class) forIndexPath:indexPath];
//    NSArray * array = self.dataArray[indexPath.section];
    HomeSerachModel * model = self.dataArray[indexPath.section][indexPath.row];
    NSMutableAttributedString * str = model.attrString;
    if (indexPath.row == 0 && indexPath.section == 0) {
        [str makeAttributeStringinser:@"热门" imageFrame:CGRectMake(8, -5, 20, 20) Index:str.length];
    }else if (indexPath.row == 1 && indexPath.section == 0){
        [str makeAttributeStringinser:@"新" imageFrame:CGRectMake(8, -5, 20, 20) Index:str.length];
        
    }
    cell.nameStr = str;
    cell.isHot = indexPath.section == 0 ? YES : NO;
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];
    
    headerView.backgroundColor = KWhiteColor;
    if (indexPath.section == 0) {
        [headerView addSubview:[self createHeaderTilteViewLeftName:@"热门搜索" rightImage:@"more" rightName:nil]];
    }else{
        [headerView addSubview:[self createHeaderTilteViewLeftName:@"历史搜索" rightImage:nil rightName:@"清空"]];
    }
    return headerView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake( KScreenWidth-32, 50);
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 32);
    if (indexPath.section == 0) {
        HomeSerachModel * model = self.dataArray[indexPath.section][indexPath.row];
        if (indexPath.row == 0 || indexPath.row == 1) {
            return CGSizeMake(model.sieze.width + 20, model.sieze.height);
        }
        return model.sieze;
    }
    return CGSizeMake(width, 32);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 12;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
#pragma mark ---懒加载
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, K_StatusBarHeight + 30 + 10, KScreenWidth-32, KScreenHeight-K_StatusBarHeight-30-10) collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[HomeSerachOneItem class] forCellWithReuseIdentifier:NSStringFromClass(HomeSerachOneItem.class)];
        [_listView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];

    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        HomeSerachModel * model1 = [[HomeSerachModel alloc] init];
        model1.title = @"厨师证怎么考级";
        HomeSerachModel * model2 = [[HomeSerachModel alloc] init];
        model2.title = @"拔丝地瓜的教程";
        HomeSerachModel * model3 = [[HomeSerachModel alloc] init];
        model3.title = @"紫薯蛋挞做法图解";
        HomeSerachModel * model4 = [[HomeSerachModel alloc] init];
        model4.title = @"国产破壁机和进口";
        HomeSerachModel * model5 = [[HomeSerachModel alloc] init];
        model5.title = @"紫薯蛋挞做法图解";
        HomeSerachModel * model6 = [[HomeSerachModel alloc] init];
        model6.title = @"国产破壁机和进口";

        HomeSerachModel * model7 = [[HomeSerachModel alloc] init];
        model7.title = @"鱼香肉丝";
        HomeSerachModel * model8 = [[HomeSerachModel alloc] init];
        model8.title = @"麻婆豆腐";
        HomeSerachModel * model9 = [[HomeSerachModel alloc] init];
        model9.title = @"二级厨师证";
        HomeSerachModel * model10 = [[HomeSerachModel alloc] init];
        model10.title = @"法国蓝带学院";

        NSArray * onearray = @[model1,model2,model3,model4,model5,model6];
        NSArray * twoArray = @[model7,model8,model9,model10];
        _dataArray =[[NSMutableArray alloc]init];
        [_dataArray addObject:onearray];
        [_dataArray addObject:twoArray];

    }
    return _dataArray;
}

/**
 设置搜索栏
 */
-(void)setSerachView{
    UIView * view = [self createSerbar];
    
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(K_StatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
}
-(UIView * )createSerbar{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UIView * view1 = UIView.new;
    view1.backgroundColor = UIColorFromRGB(0xF5F7F5);
    ViewRadius(view1, 4);
    
    [view addSubview:view1];
    self.rightBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBUtton setTitle:@"取消" forState:UIControlStateNormal];
    [self.rightBUtton setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    self.rightBUtton.titleLabel.font = SYSTEMFONT(16);
    [view addSubview:self.rightBUtton];
    
    kWeakSelf(weakSelf);
    [self.rightBUtton addActionHandler:^{
        [weakSelf backBtnClicked];
    }];
    
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-64);
        make.height.mas_equalTo(30);
    }];
    
    UIImageView * leftImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"ic_nav_search")];
    [view1 addSubview:leftImage];
    [view1 addSubview:self.serachField];
    
    
    [leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(8);
        make.width.height.mas_equalTo(20);
    }];
    
    [self.serachField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(leftImage.mas_right).offset(4);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-4);
    }];
//    self.serachField.backgroundColor = KRedColor;
    [self.rightBUtton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
//        make.height.mas_equalTo(20);
//        make.width.mas_equalTo(32);
        make.centerY.mas_equalTo(0);
    }];
    
    
    return view;
}

-(UITextField*)serachField{
    if(!_serachField){
        _serachField=[UITextField new];
        _serachField.placeholder = @"热门搜索、热门搜索";
        _serachField.userInteractionEnabled = YES;
        _serachField.placeholderColor = UIColorFromRGB(0x959E97);
        _serachField.placeholderFont = PingFangSC_Light(14);
    }
    return _serachField;
}
-(UIView * )createHeaderTilteViewLeftName:(NSString *)leftName rightImage:(NSString *)rightImage rightName:(NSString *)rightName{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,  KScreenWidth-32, 50)];
    
    UILabel * leftLabel = UILabel.new;
    leftLabel.text = leftName;
    leftLabel.font = PingFangSC_Medium(14);
    leftLabel.textColor = UIColorFromRGB(0x454F44);
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if (rightImage == nil) {
        [rightBtn setTitle:rightName forState:UIControlStateNormal];
        [rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
        rightBtn.titleLabel.font = PingFangSC_Regular(11);

    }else{
        [rightBtn setBackgroundImage:IMAGE_NAMED(rightImage) forState:UIControlStateNormal];
    }
    
    [view addSubview:leftLabel];
    [view addSubview:rightBtn];
    
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];

    return view;
}
@end

@interface HomeSerachOneItem ()
/**<#属性#>*/
@property (nonatomic,strong)UILabel*nameLabel;
/**<#属性#>*/
@property (nonatomic,strong)UILabel*leftName;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*delectBtn;
@end

@implementation HomeSerachOneItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = KRedColor;
        [self addSubview:self.nameLabel];
        [self addSubview:self.leftName];
        [self addSubview:self.delectBtn];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [self.leftName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(-30);
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(-5);
        }];
        [self.delectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
        }];
    }
    return self;
}
-(void)setIsHot:(BOOL)isHot{
    _isHot = isHot;
    self.nameLabel.hidden = !isHot;
    self.leftName.hidden = isHot;
    self.delectBtn.hidden = isHot;
    
}
-(void)setNameStr:(NSMutableAttributedString *)nameStr{
    _nameStr = nameStr;
    self.nameLabel.attributedText = nameStr;
    self.leftName.attributedText = nameStr;
}
-(UILabel*)nameLabel{
    if(!_nameLabel){
        _nameLabel=[UILabel new];
        _nameLabel.textColor = UIColorFromRGB(0x454F44);
        _nameLabel.font = PingFangSC_Regular(14);
        ViewRadius(_nameLabel, 16);
        _nameLabel.backgroundColor = UIColorFromRGB(0xF5F7F5);
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

-(UILabel*)leftName{
    if(!_leftName){
        _leftName=[UILabel new];
        _leftName.textColor = UIColorFromRGB(0x273323);
        _leftName.font = PingFangSC_Regular(14);
    }
    return _leftName;
}
-(UIButton*)delectBtn{
    if(!_delectBtn){
        _delectBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_delectBtn setImage:IMAGE_NAMED(@"icon_quxiao") forState:UIControlStateNormal];
    }
    return _delectBtn;
}
@end


@implementation HomeSerachModel

-(void)setTitle:(NSString *)title{
    _title = title;
    self.attrString = [NSMutableAttributedString makeAttributeString:title Attribute:^(NSMutableDictionary * _Nonnull attributes) {
        attributes.Color(UIColorFromRGB(0x454F44));
        [attributes setValue:PingFangSC_Regular(14) forKey:NSFontAttributeName];
    }];
    
    CGSize siz= [title boundingRectWithSize:CGSizeMake(KScreenWidth-50, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:SYSTEMFONT(16),NSFontAttributeName, nil] context:nil].size;
    self.sieze =  CGSizeMake(siz.width+28, siz.height+12) ;
}

@end
